<%--
  Created by IntelliJ IDEA.
  User: 王韫琛
  Date: 2021/1/27
  Time: 1:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>普通管理员维护公告</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/layui/2.5.6/css/layui.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/layui/2.5.6/layui.all.js">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/layui/2.5.6/layui.js">
</head>
<body>
<div style="text-align: center;display: none" id="form_body">
    <br>
    <fieldset class="layui-elem-field layui-field-title">
        <legend>普通管理员修改公告</legend>
    </fieldset>
    <br>
    <div class="layui-inline">
        <form  class="layui-form layui-form-pane" lay-filter="dataFrm" method="post">
            <div class="layui-form-item">
                <input type="hidden" name="id">
                <label class="layui-form-label" for="author">发布者姓名:</label>
                <div class="layui-input-block">
                    <input type="text" name="author" id="author" class="layui-input" required lay-verify="required"
                           lay-reqText="请输入发布者姓名" placeholder="请输入发布者姓名" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">发布者身份:</label>
                <div class="layui-input-block" >
                    <select name="grade" lay-filter="aihao" disabled="disabled">
                        <option value="">请选择</option>
                        <option value="超级管理员">超级管理员</option>
                        <option value="普通管理员">普通管理员</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="title">公告标题:</label>
                <div class="layui-input-block">
                    <input type="text" name="title" id="title" class="layui-input" required lay-verify="required"
                           lay-reqText="请输入公告标题" placeholder="请输入公告标题" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="test1">添加时间:</label>
                <div class="layui-input-block">
                    <input type="text" name="add_time" required lay-verify="required|data" class="layui-input"
                           lay-reqText="请输入日期" placeholder="请输入日期" id="test1">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label" for="content">公告内容:</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入内容" id="content" name="content" class="layui-textarea"
                                  style="width: 775px;height: 270px"></textarea>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn" lay-submit lay-filter="doSubmit" lay-event="add">修改</button>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <button type="reset" class="layui-btn layui-btn-warm">重置</button>
            </div>
        </form>
    </div>
</div>
<%--根据名字搜索开始--%>
<br>
<form class="layui-form layui-form-pane" method="post">
    <label for="authors">输入姓名搜索：</label>
    <div class="layui-inline">
        <input class="layui-input" name="author" id="authors" lay-verify="required">
    </div>
    <button class="layui-btn" lay-submit lay-filter="doSearch">搜索</button>
</form>
<%--根据名字搜索结束--%>
<table class="layui-hide" id="test" lay-filter="test"></table>
<%--表格渲染结束--%>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/javascript" src="${pageContext.request.contextPath}/webjars/layui/2.5.6/layui.js"></script>

<script>
    layui.use(['form', 'jquery', 'table', 'laydate', 'layer'], function () {
        var form = layui.form;
        var $ = layui.jquery;
        var table = layui.table;
        var layer = layui.layer;
        var laydate = layui.laydate;
        //日期控件
        laydate.render({
            elem: '#test1' //指定元素
            ,format: 'yyyy-MM-dd'
        });
        //表单渲染开始
        var tableIns = table.render({
            elem: '#test'
            , height: 524
            , url: '/AdminFindPage'
            , page: true //开启分页
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print']
            , title: '用户数据表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: 'ID', width: 80, fixed: 'left', unresize: true, sort: true}
                , {field: 'author', title: '发布者姓名', width: 120}
                , {field: 'grade', title: '发布者身份',width: 120, align: "center"}
                , {field: 'title', title: '公告标题', width: 160,align: "center"}
                , {field: 'content', title: '公告内容', align: "center"}
                , {field: 'add_time', title: '添加时间', width: 200, align: "center"}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 120,align: "center"}
            ]]
            , request: {
                pageName: 'pageNum' // 页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            , id: 'testReload'
        });
        //表单渲染结束

        //头工具栏事件开始
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;
            }
        });
        //头工具栏事件结束

        //监听行工具事件开始
        table.on('tool(test)', function (obj) {
            switch (obj.event) {
                //编辑
                case 'edit':
                    openUpdateWindow(obj.data);
                    break;
                //删除
                case 'del':
                    deleteById(obj.data);
                    break;
            }
        });
        //监听行工具事件结束

        var mainIndex;//窗口索引
        var url;//提交地址
        //更新用户信息开始
        function openUpdateWindow(data) {
            console.log(data)
            mainIndex = layer.open({
                type: 1,//弹出层类型
                title: "修改公告信息",
                area: ['800px', '750px'],
                content: $("#form_body"),//引用的窗口代码

                success: function () {
                    //表单数据回显
                    form.val("dataFrm", data);
                    //修改请求
                    url = "/AdminUpdate";
                }
            });
        }
        //更新用户信息结束
        //删除用户开始
        function deleteById(data) {
            //提示用户确认是否删除
            layer.confirm("确定要删除么", {icon: 3, title: "提示"}, function (index) {
                //发送ajax请求
                $.post("/AdminDeleteById", {"id": data.id}, function (result) {
                    if (result.success) {
                        layer.alert(result.message, {icon: 1});
                        //刷新数据表格
                        tableIns.reload();
                    } else {
                        layer.alert(result.message, {icon: 2});
                    }
                }, "json");
                //关闭提示框
                layer.close(index);
            });
        }
        //删除用户信息结束

        //监听表单提交事件开始
        form.on("submit(doSubmit)", function (data) {

            $.post(url, data.field, function (result) {
                if (result.success) {
                    layer.alert(result.message, {icon: 1});
                    //关闭窗口
                    layer.close(mainIndex);
                    //刷新数据表格
                    tableIns.reload();
                } else {
                    layer.alert(result.message, {icon: 2});
                }
            }, "json");
            return false;
        });
        //监听表单提交事件结束

        //监听搜索按钮得提交开始
        form.on("submit(doSearch)", function (data) {
            tableIns.reload({
                url: '/findAdminPageByAuthor',
                where: data.field,//查询条件
                page: {
                    curr: 1
                }
            })
            //禁止页面刷新
            return false;
        })
        //监听搜索按钮结束
    });
</script>

</body>
</html>
